package pj.model;


import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wcl
 * @date 2022/4/11
 * Created by admin on 2022/4/11 9:42
 */
public class Result implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer code = 0;

    private String msg = "";

    private Object data;

    public Result() {
    }

    public Integer getCode() {
        return code;
    }

    public Result code(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public Result data(Object data) {
        this.data = data;
        return this;
    }

    public Result data(String key, Object data) {
        Map<String, Object> map = null;
        if(this.data != null && this.data instanceof Map){
            map = (Map<String, Object>) this.data;
        }else{
            map = new HashMap<>(16);
        }
        map.put(key, data);
        this.data = map;
        return this;
    }

    private Result(Integer code) {
        this.code = code;
    }

    private Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(Status status, Object data) {
        this.code = status.code();
        this.msg = status.msg();
        this.data = data;
    }

    private Result(Object data) {
        this.data = data;
    }

    private Result(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static Result isOk() {
        return new Result(Status.OK.code(), Status.OK.msg());
    }

    public static Result ok() {
        return new Result(Status.OK.code(), Status.OK.msg());
    }

    public static Result ok(Object data) {
        return new Result(Status.OK,data);
    }

    public static Result ok(Status status,Object data) {
        return new Result(status,data);
    }

    public static Result ok(Integer code, String msg, Object data) {
        return new Result(code, msg, data);
    }

    public static Result isError() {
        return new Result(Status.ERROR.code(), Status.ERROR.msg());
    }

    public static Result fail() {
        return new Result(Status.ERROR.code(), Status.ERROR.msg());
    }

    public static Result fail(Status status) {
        return new Result(status.code(), status.msg());
    }

    public static Result fail(Integer code, String msg) {
        return new Result(code, msg);
    }

    public static Result data(Status status, Object data) {
        return new Result(status.code(), status.msg(), data);
    }

}
